<template>
  <div style="margin-top: 160px;margin-left: 330px;width: 300px;">

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="bName">
        <el-input v-model="ruleForm.bName"></el-input>
      </el-form-item>
      <el-form-item label="介绍" prop="bMsg">
        <el-input v-model="ruleForm.bMsg"></el-input>
      </el-form-item>

      <el-form-item label="管理员">
        <el-select v-model="ruleForm.dId" placeholder="请选择管理员">
          <el-option
              v-for="item in dormitoryAddList"
              :key="item.did"
              :label="item.dname"
              :value="item.did"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "BuildingAdd",
  data(){
    return{
      dormitoryAddList:'',
      ruleForm:{
        bName:'',
        bMsg:'',
        dId:''
      },
      rules: {
        bName: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, message: '*号楼', trigger: 'blur' }
        ],
        bMsg: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    submitForm(formName) {
      const _this=this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.get('http://192.168.163.234:8081/building/save', {params:_this.ruleForm}).then(
              function (response) {
                if (response.data.code==0){
                  _this.$alert(_this.ruleForm.bName+'添加成功!','',{
                    confirmButtonText:'确定',
                    callback:action => {
                      _this.$router.push('/BuildingManager')
                    }
                  })
                }
              })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this=this;
    axios.get('http://192.168.163.234:8081/dormitoryAdmin/adminList').then(
        function (response) {
          _this.dormitoryAddList=response.data.data;
        }
    )
  }
}
</script>

<style scoped>

</style>
